<?php
    // 8/29/2009: Immanuel192 rewrite this code

    /**
    * Change logs
    * Feb 11, 2011 - trungdt - Add support featuretab and Banner views
    * Feb 24, 2011 - trungdt - Temporary Disable linklist search support
    * Sep 6, 2011 - trungdt - add Show Defautl view on Administrator mode or not
    *   
    */

    class linklistmodule {
        function name() { return "Link List"; }
        function description() { return "Manage a set of links."; }
        function author() { return "ABSoft.vn"; }

        function hasSources() { return true; }
        function hasContent() { return true; }
        function hasViews() { return true; }

        function supportsWorkflow() { return false; }

        function permissions($internal = "") {
            return array(
            'administrate'=>'Administrate',
            'configure'=>'Configure',
            'create'=>'Create Links',
            'edit'=>'Edit Links',
            'delete'=>'Delete Links'
            );
        }

        function show($view,$loc = null, $title = "") {
            global $db;
            global $browser;
            global $sectionObj;

            $config = $db->selectObject('linklistmodule_config',"location_data='".serialize($loc)."'");
            if ($config == null) {
                $config->orderby = 'name';
                $config->orderhow = 0; // Ascending
                $config->remove_body_html=0;
                $config->listing_type=0;
                $config->first_link_as_title=0;
                $config->item_width=100;
                $config->item_per_page=20;
                $config->item_at_firstpage=0;
            }

            $sortby = "";

            switch ($config->orderhow) {
                case 0:
                    $sortby = " ORDER BY name ASC ";
                    break;
                case 1:
                    $sortby = " ORDER BY name DESC ";
                    break;
                case 2:
                    $sortby = " ORDER BY RAND() ";
                    break;
                case 3:
                    //sort the listings by their rank
                    $sortby = " ORDER BY rank ASC ";
                    break;
                case 4:
                    //sort the listings by their rank
                    $sortby = " ORDER BY rank DESC ";
                    break;
            }

            $limit = "";

            if ($config->item_at_firstpage > 0 && $view !="New_right_menu")
                $limit=" LIMIT 0, {$config->item_at_firstpage}";

            $links = $db->selectObjects('linklist_link',"location_data='".serialize($loc)."' {$sortby} {$limit}");


            foreach ($links as $key=>$value){
                // check file id
                if ($value->file_id <= 0) {
                    $value->picpath = '';
                } else {
                    $file = $db->selectObject('file', 'id='.$value->file_id);
                    $value->picpath = "/".$file->directory.'/'.$file->filename;
                    $links[$key]=$value;
                }

                // Remove html when confiugred
                if ($config->remove_body_html){
                    $value->description=_ab_safe_html_string($value->description);
                }
                else
                {

                }

                // URL type : /xx_abc_def
                // which is: xx : linklist_link id
                //Sep 28, 2012 sonhb, fix sef link
                if($value->sef_name=="")
                    $value->seo_link = $value->id."_"._to_vietnamese_with_nomark($value->name);
                else
                    $value->seo_link = $value->id."_".$value->sef_name;// June 18, 2012 add date
                //$value->date_text  = strftime(DISPLAY_DATE_FORMAT, $value->post_date);
                $value->date_text = _ab_dateFormat($value->post_date, true, false);
            }

            $template = new template('linklistmodule',$view,$loc);
            //Oct 24,2012 - sonhb - Return page for mobile listlink mobile view
            if($browser->isMobile())
            {
                if($view='Kutulus_ListLinks')
                {
                    $template->assign('returnpage',$sectionObj->sef_name); 
                }
            }

            // Sep 6, 2011 - trungdt - add feature: first link as title - begin
            if ($config->first_link_as_title == 1){
                if (count($links) > 0)
                {
                    $temp=array_shift($links);
                    $template->assign('first_item',$temp);   
                }
                else
                {
                    // in case that we dont have any item as first item,
                    // set it to zero
                    $template->assign('first_item',0);   
                }
            }

            //template for team
            if($_REQUEST['action']=='team_detail'||$_REQUEST['action']=='ourteam_list')
            $template->assign('team',1);
            // Sep 6, 2011 - trungdt - add feature: first link as title - end         
            $template->assign('config',$config);
            $template->assign('name_multiline',$config->name_multiline);
            $template->assign('item_width',$config->item_width);
            $template->assign('moduletitle',$title);
            $template->assign('links',$links);
            $template->register_permissions(array('administrate','configure','create','edit','delete'),$loc);
            $template->output();
        }

        function deleteIn($loc) {
            global $db;
            $db->delete('linklist_link',"location_data='".serialize($loc)."'");
        }

        function copyContent($oloc,$nloc) {
            foreach ($db->selectObjects('linklist_link',"location_data='".serialize($oloc)."'") as $l) {
                $l->location_data = serialize($nloc);
                $db->insertObject($l,'linklist_link');
            }
        }

        function searchName() {
            return exponent_lang_loadKey('modules/linklistmodule/class.php','search_name');
        }

        /*
        8/29/2009 immanuel192
        This function create HTML content of spider item
        */
        function spiderTemplate($album)
        {
            global $db;


            // create template content
            $template=new template("linklistmodule","_search");
            $template->assign("album",$album);
            ob_start();
            $template->output();
            $get_value=ob_get_contents();
            ob_end_clean(); 
            return $get_value;
        }

        function spiderContent($item = null) {
            global $db;

            if (!defined('SYS_SEARCH')) include_once(BASE.'subsystems/search.php');

            $search = null;
            $search->title = '';
            $search->view_link = '';
            $search->category = exponent_lang_loadKey('modules/linklistmodule/class.php','search_post_type');
            $search->ref_module = 'linklistmodule';
            $search->ref_type = 'linklist_link';

            if ($item) {
                $db->delete('search',"ref_module='linklistmodule' AND ref_type='linklist_link' AND original_id=" . $item->id);

                $search->original_id = $item->id;

                $search->lang = exponent_lang_getLang_from_Object($item->location_data);
                if ($search->lang == "")
                    return;

                // get config
                $config = $db->selectObject('linklistmodule_config',"location_data='{$item->location_data}'");
                // no search
                if (isset($config->no_search) && ($config->no_search == 1))
                    return;

                $search->body = ' ' . exponent_search_removeHTML($item->description) . ' ';

                $search->title = $item->name;
                if ($item->detail_title != "")
                    $search->title = $item->detail_title;

                if (trim($search->title) == ""){
                    // find title
                    $temp=$db->selectObject("container","internal = '{$item->location_data}'");
                    if ($temp!=null)
                        $search->title=$temp->title;
                }

                $search->title = " ".$search->title." ";

                // view link
                $temp=unserialize($item->location_data);

                if ($item->view_style == 0) // link to URL
                {
                    $search->view_link=$item->url;

                    // if No URL defined
                    if ($item->url == "http://" || $item->url =="")
                    {
                        $temp=$db->selectObject("sectionref","(source = '{$temp->src}') AND (module='linklistmodule')");
                        if ($temp!=null)
                        {
                            $params=array("section"=>$temp->section);
                            $search->view_link=exponent_core_makeLink($params)."#".$item->name;
                        }
                        else
                        {
                            $search->view_link="javascript: void(0);";
                        }
                    }
                }
                else
                {
                    // show content
                    $params=array(
                    "module"=>$temp->mod,
                    "action"=>"detail",
                    "src"=>$temp->src,
                    "name"=> $item->id."_"._to_vietnamese_with_nomark($item->name)
                    );
                    $search->view_link=exponent_core_makeLink($params);
                }

                $search->location_data = $item->location_data;
                $db->insertObject($search,'search');

            } else {
                $db->delete('search',"ref_module='linklistmodule' AND ref_type='linklist_link'");
                foreach ($db->selectObjects('linklist_link') as $item) {
                    $search->original_id = $item->id;

                    $search->lang = exponent_lang_getLang_from_Object($item->location_data);
                    if ($search->lang == "")
                        continue;

                    // get config
                    $config = $db->selectObject('linklistmodule_config',"location_data='{$item->location_data}'");
                    // no search
                    if (isset($config->no_search) && ($config->no_search == 1))
                        continue;

                    $search->body = ' ' . exponent_search_removeHTML($item->description) . ' ';

                    $search->title = $item->name;
                    if ($item->detail_title != "")
                        $search->title = $item->detail_title;

                    if (trim($search->title) == ""){
                        // find title
                        $temp=$db->selectObject("container","internal = '{$item->location_data}'");
                        if ($temp!=null)
                            $search->title=$temp->title;
                    }

                    $search->title = " ".$search->title." ";

                    // view link
                    $temp=unserialize($item->location_data);

                    if ($item->view_style == 0) // link to URL
                    {
                        $search->view_link=$item->url;

                        // if No URL defined
                        if ($item->url == "http://" || $item->url =="")
                        {
                            $temp=$db->selectObject("sectionref","(source = '{$temp->src}') AND (module='linklistmodule')");
                            if ($temp!=null)
                            {
                                $params=array("section"=>$temp->section);
                                $search->view_link=exponent_core_makeLink($params)."#".$item->name;
                            }
                            else
                            {
                                $search->view_link="javascript: void(0);";
                            }
                        }
                    }
                    else
                    {
                        // show content
                        $params=array(
                        "module"=>$temp->mod,
                        "action"=>"detail",
                        "src"=>$temp->src,
                        "name"=> $item->id."_"._to_vietnamese_with_nomark($item->name)
                        );
                        $search->view_link=exponent_core_makeLink($params);
                    }

                    $search->location_data = $item->location_data;
                    $db->insertObject($search,'search');
                }
            }

            return true;
        }

        /*
        Feb 24, 2011 - trungdt - Temporary disable linklist search support
        function spiderContent($item = null) {
        global $db;

        if (!defined('SYS_SEARCH')) require_once(BASE.'subsystems/search.php');

        $search = null;
        $search->category = 'Links';
        $search->ref_module = 'linklistmodule';
        $search->ref_type = 'linklist_link';
        $search->view_link = '';

        if ($item) {
        $db->delete('search',"ref_module='linklistmodule' AND ref_type='linklist_link' AND original_id=" . $item->id);
        $search->original_id = $item->id;
        $search->title = ' ' . $item->name . ' ';
        $search->body = ' ' . exponent_search_removeHTML($item->description) . ' ';
        $search->location_data = $item->location_data;
        $db->insertObject($search,'search');
        } else {
        $db->delete('search',"ref_module='linklistmodule' AND ref_type='linklist_link'");
        foreach ($db->selectObjects('linklist_link') as $item) {
        $search->original_id = $item->id;
        $search->title = ' ' . $item->name . ' ';
        $search->body = ' ' . exponent_search_removeHTML($item->description) . ' ';
        $search->location_data = $item->location_data;
        $db->insertObject($search,'search');
        }
        }

        return true;
        }
        */
    }

?>
